﻿using AiXiu.IDAL;
using AiXiu.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AiXiu.DAL
{
    public class UserService : IUserService
    {
        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="login"></param>
        /// <returns></returns>
        public int Register(TBLogins login)
        {
            AiXiuDbContext dbContext = new AiXiuDbContext();
            dbContext.TBLogins.Add(login);
            int n = dbContext.SaveChanges();
            if (n > 0)
            {
                TBUsers user = new TBUsers();
                user.Id = login.Id;
                user.Sex = 0;
                user.CreationTime = DateTime.Now;
                dbContext.TBUsers.Add(user);
                dbContext.SaveChanges();
            }
            return n;
        }


        /// <summary>
        /// 用户名是否已注册
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <returns></returns>
        public bool ExistUserName(string userName)
        {
            AiXiuDbContext dbContext = new AiXiuDbContext();
            return dbContext.TBLogins.Any(m => m.UserName == userName);
        }

        /// <summary>
        /// 手机号是否已注册
        /// </summary>
        /// <param name="mobileNumber">手机号</param>
        /// <returns></returns>
        public bool ExistMobileNumber(string mobileNumber)
        {
            AiXiuDbContext dbContext = new AiXiuDbContext();
            return dbContext.TBLogins.Any(m => m.MobileNumber == mobileNumber);
        }

        /// <summary>
        /// 通过用户名查找用户信息
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <returns></returns>
        public TBLogins FindByUserName(string userName)
        {
            AiXiuDbContext dbContext = new AiXiuDbContext();
            //SingleOrDefault() 返回序列中满足指定条件的唯一元素；如果这类元素不存在，则返回默认值
            TBLogins login = dbContext.TBLogins.SingleOrDefault(a => a.UserName == userName);
            return login;
        }

        /// <summary>
        /// 通过用户编号查找用户的详细信息
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public TBUsers FindByUserId(int userId)
        {
            AiXiuDbContext dbContext = new AiXiuDbContext();
            //return dbContext.TBUsers.Find(userId);
            return dbContext.TBUsers.SingleOrDefault(m => m.Id == userId);
        }

        /// <summary>
        /// 通过手机号查找用户
        /// </summary>
        /// <param name="mobile"></param>
        /// <returns></returns>
        public TBLogins FindByMobile(string mobile)
        {
            AiXiuDbContext context = new AiXiuDbContext();
            TBLogins login= context.TBLogins.SingleOrDefault(a => a.MobileNumber == mobile);
            return login;
        }

        /// <summary>
        /// 更新用户信息(不包含头像)
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public TBUsers EditUser(TBUsers user)
        {
            // 查询数据库
            AiXiuDbContext dbContext = new AiXiuDbContext();
            TBUsers userEntity = dbContext.TBUsers.SingleOrDefault(m => m.Id == user.Id);
            if (userEntity == null)
            {
                return null;
            }
            userEntity.NickName = user.NickName;
            userEntity.Sex = user.Sex;
            userEntity.Birthday = user.Birthday;
            userEntity.Address = user.Address;
            userEntity.Hobby = user.Hobby;
            // 更新到数据库
            dbContext.Entry(userEntity).State = EntityState.Modified;
            dbContext.SaveChanges();
            // 返回结果
            return userEntity;
        }

        /// <summary>
        /// 编辑头像
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public TBUsers EditAvatar(TBUsers user)
        {
            // 查询数据库
            AiXiuDbContext dbContext = new AiXiuDbContext();
            TBUsers userEntity = dbContext.TBUsers.SingleOrDefault(m => m.Id == user.Id);
            if (userEntity == null)
            {
                return null;
            }
            userEntity.Avatar = user.Avatar;
            // 更新到数据库
            dbContext.Entry(userEntity).State = EntityState.Modified;
            dbContext.SaveChanges();
            // 返回结果
            return userEntity;
        }


        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <returns></returns>
        public List<TBUsers> GetList()
        {
            AiXiuDbContext dbContext = new AiXiuDbContext();
            return dbContext.TBUsers.OrderByDescending(a=>a.CreationTime).ToList();
        }
    }
}
